<?php
namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Psr\Container\ContainerInterface;

use App\Facade\Event;
use App\Exception\{ErrorException, Message};

abstract class Controller
{
    /**
     * @Inject
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject
     * @var ResponseInterface
     */
    protected $response;
    
    // 显示信息
    public function message($message)
    {
        throw new Message($message);
    }
    
    public function error($message)
    {
        throw new ErrorException($message);
    }
    
    // 获取请求
    public function input($name)
    {
        return $this->request->input($name);
    }
    
    // 获取请求组
    public function inputs(...$names)
    {
        $data = [];
        
        foreach($names as $name)
        {
            $input = $this->input($name);
            
            if(isset($input))
            {
                $data[$name] = $input;
            }
        }
        
        return $data;
    }
    
    // 事件
    public function event($event)
    {
        return Event::dispatch($event);
    }
}
